package com.example.wwk.graduationpractice.Adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.wwk.graduationpractice.Activity.SettingChangePwdActivity;
import com.example.wwk.graduationpractice.Activity.SettingPortraitActivity;
import com.example.wwk.graduationpractice.NetDataUtils.MessagesDataUtils;
import com.example.wwk.graduationpractice.Globalutils.MyApplication;
import com.example.wwk.graduationpractice.R;
import com.example.wwk.graduationpractice.Bean.SettingBean;
import com.example.wwk.graduationpractice.Globalutils.PromptShowUtils;

import java.util.List;

/**
 * Created by wwk on 2017/3/25.
 */

public class SettingAdapter extends RecyclerView.Adapter<SettingAdapter.ViewHolder> {

    private List<SettingBean> mList;
    private Context mcontext;
    private String userName;

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView settingName;
        View mView;

        public ViewHolder(View itemView) {
            super(itemView);
            mView = itemView;
            settingName = (TextView) itemView.findViewById(R.id.setting_item_setting_name);
        }
    }

    public SettingAdapter(List<SettingBean> list, String user) {
        mList = list;
        userName=user;
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (mcontext == null) {
            mcontext = parent.getContext();
        }
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.setting_item, parent, false);
        final ViewHolder holder = new ViewHolder(view);
        holder.mView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {//item点击事件
                int position = holder.getAdapterPosition();
                SettingBean setting = mList.get(position);
                switch (setting.getSettingName()){
                    case SettingBean.setting1:
                        Intent intent = new Intent(MyApplication.getContext(), SettingPortraitActivity.class);
                        intent.putExtra("user",userName);
                       mcontext.startActivity(intent);
                        break;
                    case SettingBean.setting2:
                        MessagesDataUtils.deleteWhoHistory(userName);
                        PromptShowUtils.showSnackbar(view,"清除成功！");
                        break;
                    case SettingBean.setting3:
                        Intent intent1 = new Intent(MyApplication.getContext(), SettingChangePwdActivity.class);
                        mcontext.startActivity(intent1);
                        break;
                    default:
                        break;
                }

            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(SettingAdapter.ViewHolder holder, int position) {
        SettingBean settingBean = mList.get(position);
        holder.settingName.setText(settingBean.getSettingName());

    }

    @Override
    public int getItemCount() {
        return mList.size();
    }
}
